package com.serkol.ams.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.serkol.ams.bean.MyPage;
import com.serkol.ams.bean.MyPageOld;
import com.serkol.ams.entity.LoginLog;
import com.serkol.ams.entity.UsersWalletWithdrawal;
import com.serkol.ams.mapper.UsersWalletWithdrawalMapper;
import com.serkol.ams.modal.SqlFeildModal;
import com.serkol.ams.service.UsersBankService;
import com.serkol.ams.service.UsersWalletWithdrawalService;
import com.serkol.constant.AuthConstant;
import com.serkol.handler.MyLambdaQueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author pingan
 * @description 针对表【users_wallet_withdrawal】的数据库操作Service实现
 * @createDate 2022-09-28 12:31:45
 */
@Service
public class UsersWalletWithdrawalServiceImpl extends ServiceImpl<UsersWalletWithdrawalMapper, UsersWalletWithdrawal>
        implements UsersWalletWithdrawalService {
    @Autowired
    private UsersBankService usersBankService;

    @Override
    public MyPageOld findByMyWithdrawal(int pageSize, int current, int status) {
        LoginLog loginLog = AuthConstant.getLoginLog();
        QueryWrapper<UsersWalletWithdrawal> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(UsersWalletWithdrawal::getUserId, loginLog.getUserId()).orderByDesc(UsersWalletWithdrawal::getCreateDate);
        if (status > 0) {
            queryWrapper.lambda().eq(UsersWalletWithdrawal::getStatus, status);
        }
        Page<UsersWalletWithdrawal> page = new Page<>(current, pageSize);
        IPage<UsersWalletWithdrawal> data = this.baseMapper.selectPage(page, queryWrapper);

        return MyPageOld.parsePage(data);
    }

    @Override
    public MyPage<Map<String, Object>> findByPageList(int pageSize, int current, int status, String name) {
        MyLambdaQueryWrapper<UsersWalletWithdrawal> queryWrapper = new MyLambdaQueryWrapper<>();
        queryWrapper.select(UsersWalletWithdrawal::getId, UsersWalletWithdrawal::getName, UsersWalletWithdrawal::getAmount, UsersWalletWithdrawal::getTaxes, UsersWalletWithdrawal::getServiceCharge, UsersWalletWithdrawal::getRequestAmount, UsersWalletWithdrawal::getBalance, UsersWalletWithdrawal::getBalanceOld, UsersWalletWithdrawal::getRemark, UsersWalletWithdrawal::getPasswortType, UsersWalletWithdrawal::getPayType, UsersWalletWithdrawal::getSn, UsersWalletWithdrawal::getPaySn, UsersWalletWithdrawal::getStatus, UsersWalletWithdrawal::getType, UsersWalletWithdrawal::getCreateDate, UsersWalletWithdrawal::getUpdateDate);
        queryWrapper.authName(UsersWalletWithdrawal::getUserId).as("authName").authName(UsersWalletWithdrawal::getOperatorId).as("operatorName");
        if (status > 0) {
            queryWrapper.eq(UsersWalletWithdrawal::getStatus, status);
        }
        if (StringUtils.isNotEmpty(name)) {
            queryWrapper.inSql(UsersWalletWithdrawal::getUserId, SqlFeildModal.getAuthNameLike(name));
        }

        Map<String, Object> userBankQueary = new HashMap<>();
        userBankQueary.put("ID", "BANK_ID");
        queryWrapper.otherTableColmuns("banks", "NAME", userBankQueary).as("backName");

        Map<String, Object> userBankInfoQueary = new HashMap<>();
        userBankInfoQueary.put("ID", "BANK_CARD_ID");
        queryWrapper.otherTableColmuns("users_bank", "NAME", userBankInfoQueary).as("backUserName");
        queryWrapper.otherTableColmuns("users_bank", "CARD_NO", userBankInfoQueary).as("backCardNo");
        queryWrapper.otherTableColmuns("users_bank", "ADDRESS", userBankInfoQueary).as("backAddress");
        queryWrapper.otherTableColmuns("users_bank", "PHONE", userBankInfoQueary).as("backPhone");



        queryWrapper.orderByDesc(UsersWalletWithdrawal::getCreateDate);

        Page<Map<String, Object>> page = new Page<>(current, pageSize);
        IPage<Map<String, Object>> data = this.baseMapper.selectMapsPage(page, queryWrapper);

        return MyPage.parsePage(data);
    }


    @Override
    public Map<String, Object> getInfoLog() {
        LoginLog loginLog = AuthConstant.getLoginLog();
        Map<String, Object> map = new HashMap<>();
        Date date = DateUtils.addDays(new Date(), -7);
        MyLambdaQueryWrapper<UsersWalletWithdrawal> queryWrapper = new MyLambdaQueryWrapper<>();
        queryWrapper.between(UsersWalletWithdrawal::getCreateDate, date, new Date());
        //  queryWrapper.sum(UsersWalletWithdrawal::getRequestAmount).as("amount");
        queryWrapper.last("limit 1");
        queryWrapper.eq(UsersWalletWithdrawal::getUserId, loginLog.getUserId());
        Long week = this.count(queryWrapper);

        if (week == null) {
            week = Long.valueOf("0");
        }

        Date date1 = DateUtils.addDays(new Date(), -30);
        MyLambdaQueryWrapper<UsersWalletWithdrawal> queryWrapper1 = new MyLambdaQueryWrapper<>();
        queryWrapper1.between(UsersWalletWithdrawal::getCreateDate, date1, new Date());
        //     queryWrapper1.sum(UsersWalletWithdrawal::getRequestAmount).as("amount");
        queryWrapper1.last("limit 1");
        queryWrapper1.eq(UsersWalletWithdrawal::getUserId, loginLog.getUserId());
        Long manth = this.count(queryWrapper1);

        if (manth == null) {
            manth = Long.valueOf("0");
        }

        map.put("week", week);
        map.put("manth", manth);

        return map;
    }

    @Override
    public Map<String, Object> getWithdrawalInfo(long id) {

        MyLambdaQueryWrapper<UsersWalletWithdrawal> queryWrapper = new MyLambdaQueryWrapper<>();
        queryWrapper.eq(UsersWalletWithdrawal::getId, id)
                .select(UsersWalletWithdrawal::getBankCardId,UsersWalletWithdrawal::getAuditRemark,UsersWalletWithdrawal::getAuditSn,UsersWalletWithdrawal::getId, UsersWalletWithdrawal::getName, UsersWalletWithdrawal::getAmount, UsersWalletWithdrawal::getTaxes,UsersWalletWithdrawal::getAuditImage, UsersWalletWithdrawal::getServiceCharge, UsersWalletWithdrawal::getRequestAmount, UsersWalletWithdrawal::getBalance, UsersWalletWithdrawal::getBalanceOld, UsersWalletWithdrawal::getRemark, UsersWalletWithdrawal::getPasswortType, UsersWalletWithdrawal::getPayType, UsersWalletWithdrawal::getSn, UsersWalletWithdrawal::getPaySn, UsersWalletWithdrawal::getStatus, UsersWalletWithdrawal::getType, UsersWalletWithdrawal::getCreateDate, UsersWalletWithdrawal::getUpdateDate);
        queryWrapper.authName(UsersWalletWithdrawal::getUserId).as("authName").authName(UsersWalletWithdrawal::getOperatorId).as("operatorName").
                picture(UsersWalletWithdrawal::getAuditImage).as("auditImageUrl").userAvatar(UsersWalletWithdrawal::getUserId).as("avatar");
        Map<String, Object> map = this.getMap(queryWrapper);

        if (map.containsKey("bankCardId")) {
            Object bankCardId = map.get("bankCardId");
            Long aLong = Long.valueOf(bankCardId.toString());
            Map<String, Object> bankInfo = usersBankService.findById(aLong);
            map.put("bankInfo", bankInfo);
        }
        return map;
    }

    @Override
    public Map<String, Object> getUsersStastic() {
        Map<String, Object> map = new HashMap<>();
        MyLambdaQueryWrapper<UsersWalletWithdrawal> queryWrapper = new MyLambdaQueryWrapper<>();
        queryWrapper.sum(UsersWalletWithdrawal::getRequestAmount).as("price").last("LIMIT 1");
        queryWrapper.eq(UsersWalletWithdrawal::getStatus, 1);
        Map<String, Object> map2 = this.getMap(queryWrapper);
        Object price = map2.get("price");
        if (price == null) {
            price = Double.valueOf(0.0);
        }

        map.put("price", price);
        MyLambdaQueryWrapper<UsersWalletWithdrawal> queryWrapper1 = new MyLambdaQueryWrapper<>();
        queryWrapper1.sum(UsersWalletWithdrawal::getRequestAmount).as("price").last("LIMIT 1");
//WHERE (status NOT IN (2) AND state = 1) GROUP BY name ORDER BY price DESC LIMIT 20
        queryWrapper1.eq(UsersWalletWithdrawal::getStatus, 2);
        Map<String, Object> map1 = this.getMap(queryWrapper1);
        Object price1 = null;
        if (map1 != null) {
            price1 = map1.get("price");
        }
        if (price1 == null) {
            price1 = Double.valueOf(0.0);
        }
        map.put("wait", price1);
        return map;
    }

    @Override
    public UsersWalletWithdrawal findByWaitByUserId(Long userId) {
        MyLambdaQueryWrapper<UsersWalletWithdrawal> queryWrapper = new MyLambdaQueryWrapper<>();
        queryWrapper.eq(UsersWalletWithdrawal::getStatus, 2);
        queryWrapper.last("limit 1");
        queryWrapper.eq(UsersWalletWithdrawal::getUserId, userId);
        return this.getOne(queryWrapper);
    }

    @Override
    public long findByWaitByUserCount(Long userId) {
        MyLambdaQueryWrapper<UsersWalletWithdrawal> queryWrapper = new MyLambdaQueryWrapper<>();
        queryWrapper.eq(UsersWalletWithdrawal::getStatus, 2);

        queryWrapper.eq(UsersWalletWithdrawal::getUserId, userId);
        return this.count(queryWrapper);
    }
}




